<template>
  <view>
    <view style="height: 200px;background-image: url(/static/new_img/home_bg.png);background-size: 100% 100%;">
      <view style="height: 56%;display: flex;margin-left: 25rpx;">
        <image src="/static/new_img/download1.png"
          style="display: flex;margin-top: auto;margin-bottom: 0px;width:100px;height:50rpx" shape="aspectFit"></image>
      </view>
      <view style="height: 34%;display: flex;margin-left: 25rpx;">
        <view style="margin-top: 10px;">
          <text style="font-size: 14px;color: #b1b1b1;">剩余可兑换免广告时间</text>
          <text style="font-size: 18px;">{{stime}}分</text>
          <label
            style="font-size: 16px;margin-left: 25rpx; background:#fc4d06;padding: 5rpx 20rpx;color: #ffffff;border-radius: 20rpx;"
            @click="dui">兑换</label>
        </view>
      </view>
    </view>

    <view
      style="height: 55px;background-image: url(/static/new_img/rollupBg.png);background-size: 100% 100%;margin: -25px 25rpx 25rpx 15rpx;">
      <view style="height: 100%;display: flex;flex-direction: column;padding: 40upx 0 40upx 200upx;">

      </view>
    </view>

    <view>
      <view>
        <view v-if="config.task_google_status_j==1" class="aditem">
          <view>
            <image src="/static/new_img/liulangliang.png" class="logo"></image>
          </view>
          <view style="width: auto;">
            <text class="text_line" style="font-size: 15px;color: #000000;font-weight: 400;">激励任务</text>

            <text
              style="font-size: 12px;color: #939393;font-weight: 400;">点击我免除广告{{config.task_google_status_j_time}}分钟</text>
          </view>
          <view @click="doShowRewardAds" class="itembat" >领取任务</view>
        </view>
      </view>
      <view>
        <view class="aditem" v-if="config.task_d_status==1">
          <view>
            <image src="/static/new_img/liulangliang.png" class="logo"></image>
          </view>
          <view style="width: auto;">
            <text class="text_line" style="font-size: 15px;color: #000000;font-weight: 400;">重复任务</text>

            <text style="font-size: 12px;color: #939393;font-weight: 400;">每点击一个广告免除广告{{config.task_d_m}}分钟</text>
          </view>
          <view class="itemhui"><text>已领取</text></view>
        </view>
      </view>

      <view v-if="config.task_y_status==1">
        <view class="aditem">
          <view>
            <image src="/static/new_img/yd.png" class="logo"></image>
          </view>
          <view style="width: auto;">
            <text class="text_line" style="font-size: 15px;color: #000000;font-weight: 400;">阅读任务</text>

            <text
              style="font-size: 12px;color: #939393;font-weight: 400;">每阅读{{config.task_y_m}}分钟奖励免除广告{{config.task_y_time}}分钟</text>
          </view>
          <view class="itemhui"><text>已领取</text></view>
        </view>
      </view>
    </view>

  </view>
</template>

<script>
  // #ifdef APP-PLUS
  var RewardAd = uni.requireNativePlugin('Gr-GoogleAdMod_RewardAd');
  var InterstitialAd = uni.requireNativePlugin('Gr-GoogleAdMod_InterstitialAd');
  // #endif
  import time from '@/util/getdateTime.js';
  export default {
    data() {
      return {
        config: {},
        stime: 0,
        time: 0,
        hasLoadInterstitialAd: false,
        interstitialAdPlacementId: 'ca-app-pub-2669354798675201/9917261503', //激励广告测试Id
      }
    },
    onShow() {
      this.config = uni.getStorageSync('config');
      let config_time = uni.getStorageSync('config_ad_time');
      if (config_time) {
        this.stime = config_time
      }
      let data = uni.getStorageSync('bookshelf_time')
      this.time = 0
      for (let i = 0; i < data.length; i++) {
        if (data[i].is === 0) {
          this.time += data[i].time
        }
      }
      this.stime = parseInt(this.stime) + Math.floor((this.time / 60))
      this.doLoadRewardAds()
    },
    methods: {
      doLoadRewardAds(is) {
        let that = this;
        // #ifdef APP
        InterstitialAd.loadInterstitialAd(that.interstitialAdPlacementId, ret => {
          if (ret.code == 201) {
            that.hasLoadInterstitialAd = true;
            uni.hideToast();
            if(is){
              that.doShowRewardAds()
            }
            console.log('加载成功.');
          } else {
            that.hasLoadInterstitialAd = false;
            console.log('加载失败.');
            uni.hideToast();
          }
        });
        // #endif
      },

      /**
       * 显示激励广告
       */
      doShowRewardAds() {
        let that = this;
        // #ifdef APP
        if (!that.hasLoadInterstitialAd) {
           uni.showLoading({
            title: '加载中'
           });
          that.doLoadRewardAds(true)
          return
        } else {
          InterstitialAd.showInterstitialAd(ret => {
            that.hasLoadInterstitialAd = false;
            if (ret.code == 1006) {
              //开始显示广告
              uni.hideLoading();
            } else if (ret.code == 200) {
              //广告播放完成,关闭广告页面后执行

              let times = parseInt(uni.getStorageSync('config_ad_time') ? uni.getStorageSync('config_ad_time') :
                0) + parseInt(this.config.task_google_status_j_time);
              uni.setStorageSync('config_ad_time', times)
              that.hasLoadInterstitialAd = false;
            } else if (ret.code == 1008) {
              //广告未加载
              console.log('Please load the advertisement first.');

              uni.hideLoading();
            } else { //其他状态

              uni.hideLoading();
              console.log('error: ' + ret.msg);
            }
          });
        }
        // #endif
      },
      dui() {

        let data = uni.getStorageSync('bookshelf_time')
        this.time = 0
        for (let i = 0; i < data.length; i++) {
          data[i].is = 1;
        }

        if (this.stime) {
          let tt = uni.getStorageSync('config_time');
          if (tt < time.dtime()) {
            tt = 0
          }
          let st = tt ? time.timej(this.stime, tt) : time.timej(this.stime, 0)
          uni.showToast({
            title: '兑换成功',
            icon: 'none',
            duration: 2000
          });
          this.stime = 0;
          uni.setStorageSync('config_time', st)
          uni.setStorageSync('config_ad_time', 0)
          uni.setStorageSync('bookshelf_time', data)
        } else {
          uni.showToast({
            title: '剩余时间不足无法兑换!',
            icon: 'none',
            duration: 2000
          });

        }
        // this.$forceUpdate() 
      },
    }
  }
</script>

<style>
  .text_line {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
  }

  .aditem {
    background-color: #fff;
    margin-bottom: 5px;
    padding: 8px;
    display: flex;
    line-height: 50rpx;
  }

  .logo {
    width: 60rpx;
    height: 60rpx
  }

  .itembat {
    font-size: 26rpx;
    margin: auto 5px auto auto;
    border-radius: 10rpx;
    padding: 0 20rpx;
    background-color: #fc4d06;
    color: #fff;
  }

  .itemhui {
    background-color: #eeeeee;
    border-radius: 10rpx;
    color: #999393;
    font-size: 26rpx;
    margin: auto 5px auto auto;
    padding: 0 20rpx;
  }
</style>
